//*********************************************************************
//		Copyright (c) 2003 - 2008 Microsoft Corporation.
//      This code is provided under Microsoft Public License, which can be found at
//      http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx 
//
// @File: FormW4.cs
//
// Purpose: Installation & Configuration Wizard - screen 4 (Configurtion - Targets list)
//
//
//
// Notes: 
//
//
//	
// History: 
//     @Version: V2.1
//
//     08/18/08 PGV 2.1.001.0 Starting V2.1 (based on 2.0.030.0 code)
//
// @EndHeader@
//*********************************************************************

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace H2V2.ConfigUtility
{
	public class FormW4 : H2V2.ConfigUtility.FormWbase
	{
		private System.Windows.Forms.Label lTargets;
		private System.Windows.Forms.TextBox tbNewTarget;
		private System.Windows.Forms.Button btAddFromFile;
		private System.Windows.Forms.Button btRemoveTarget;
		private System.Windows.Forms.Button btAddTarget;
		private System.Windows.Forms.ListBox lbTargets;
		private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.IContainer components = null;

		public FormW4()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			string cfgFile = ((MainForm.WizardMode) ? MainForm.BinPath : "")+"H2Config.xml";
			InitConfigurator (cfgFile);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lTargets = new System.Windows.Forms.Label();
			this.tbNewTarget = new System.Windows.Forms.TextBox();
			this.btAddFromFile = new System.Windows.Forms.Button();
			this.btRemoveTarget = new System.Windows.Forms.Button();
			this.btAddTarget = new System.Windows.Forms.Button();
			this.lbTargets = new System.Windows.Forms.ListBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Name = "panel1";
			// 
			// panel2
			// 
			this.panel2.Name = "panel2";
			// 
			// btCancel
			// 
			this.btCancel.Name = "btCancel";
			// 
			// btNext
			// 
			this.btNext.Name = "btNext";
			// 
			// lTargets
			// 
			this.lTargets.Location = new System.Drawing.Point(8, 152);
			this.lTargets.Name = "lTargets";
			this.lTargets.Size = new System.Drawing.Size(64, 23);
			this.lTargets.TabIndex = 28;
			this.lTargets.Text = "Targets:";
			this.lTargets.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tbNewTarget
			// 
			this.tbNewTarget.Location = new System.Drawing.Point(424, 152);
			this.tbNewTarget.Name = "tbNewTarget";
			this.tbNewTarget.Size = new System.Drawing.Size(208, 20);
			this.tbNewTarget.TabIndex = 27;
			this.tbNewTarget.Text = "";
			this.tbNewTarget.TextChanged += new System.EventHandler(this.tbNewTarget_TextChanged);
			// 
			// btAddFromFile
			// 
			this.btAddFromFile.Location = new System.Drawing.Point(312, 232);
			this.btAddFromFile.Name = "btAddFromFile";
			this.btAddFromFile.Size = new System.Drawing.Size(88, 23);
			this.btAddFromFile.TabIndex = 26;
			this.btAddFromFile.Text = "Add From File";
			this.btAddFromFile.Click += new System.EventHandler(this.btAddFromFile_Click);
			// 
			// btRemoveTarget
			// 
			this.btRemoveTarget.Enabled = false;
			this.btRemoveTarget.Location = new System.Drawing.Point(312, 192);
			this.btRemoveTarget.Name = "btRemoveTarget";
			this.btRemoveTarget.Size = new System.Drawing.Size(88, 23);
			this.btRemoveTarget.TabIndex = 25;
			this.btRemoveTarget.Text = "Remove";
			this.btRemoveTarget.Click += new System.EventHandler(this.btRemoveTarget_Click);
			// 
			// btAddTarget
			// 
			this.btAddTarget.Enabled = false;
			this.btAddTarget.Location = new System.Drawing.Point(312, 152);
			this.btAddTarget.Name = "btAddTarget";
			this.btAddTarget.Size = new System.Drawing.Size(88, 23);
			this.btAddTarget.TabIndex = 24;
			this.btAddTarget.Text = "Add";
			this.btAddTarget.Click += new System.EventHandler(this.btAddTarget_Click);
			// 
			// lbTargets
			// 
			this.lbTargets.Location = new System.Drawing.Point(80, 144);
			this.lbTargets.Name = "lbTargets";
			this.lbTargets.Size = new System.Drawing.Size(208, 238);
			this.lbTargets.TabIndex = 23;
			this.lbTargets.SelectedIndexChanged += new System.EventHandler(this.lbTargets_SelectedIndexChanged);
			// 
			// textBox1
			// 
			this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.textBox1.Location = new System.Drawing.Point(24, 16);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(496, 72);
			this.textBox1.TabIndex = 1;
			this.textBox1.TabStop = false;
			this.textBox1.Text = "Please enter the list of computers you want to collect from";
			// 
			// FormW4
			// 
			this.AcceptButton = null;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(640, 478);
			this.Controls.Add(this.lTargets);
			this.Controls.Add(this.tbNewTarget);
			this.Controls.Add(this.btAddFromFile);
			this.Controls.Add(this.btRemoveTarget);
			this.Controls.Add(this.btAddTarget);
			this.Controls.Add(this.lbTargets);
			this.Name = "FormW4";
			this.Controls.SetChildIndex(this.panel2, 0);
			this.Controls.SetChildIndex(this.panel1, 0);
			this.Controls.SetChildIndex(this.lbTargets, 0);
			this.Controls.SetChildIndex(this.btAddTarget, 0);
			this.Controls.SetChildIndex(this.btRemoveTarget, 0);
			this.Controls.SetChildIndex(this.btAddFromFile, 0);
			this.Controls.SetChildIndex(this.tbNewTarget, 0);
			this.Controls.SetChildIndex(this.lTargets, 0);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		bool InitConfigurator (string a_filename)
		{
			try 
			{
				MainForm.Cfg.Init (a_filename);
			
				if (lbTargets.Items.Count > 0)
				{
					lbTargets.Items.Clear ();
				}
				foreach (string cmp in MainForm.Cfg.Computers_array)
				{
					lbTargets.Items.Add (cmp);
				}

				MainForm.curConfigFile = a_filename;
				return true;
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message, "Opening XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
		}

		private void btAddTarget_Click(object sender, System.EventArgs e)
		{
			if (MainForm.ValidateName (tbNewTarget.Text))
			{
				if (MainForm.Cfg.AddTarget (tbNewTarget.Text))
				{
					lbTargets.Items.Add (tbNewTarget.Text.ToUpper ());
					tbNewTarget.Text = "";
				}
				else
				{
					MessageBox.Show ("Target is already on the list", "Adding Target", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			else
			{
				MessageBox.Show ("Invalid computer name \""+tbNewTarget.Text+"\"", "Adding Target", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

		}

		private void btRemoveTarget_Click(object sender, System.EventArgs e)
		{
			string name = (string)lbTargets.SelectedItem;
			lbTargets.Items.RemoveAt (lbTargets.SelectedIndex);
			MainForm.Cfg.RemoveTarget (name);
		}

		private void btAddFromFile_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog ();

			ofd.InitialDirectory = ".";
			ofd.Filter = "txt files (*.txt)|*.txt";

			if (ofd.ShowDialog () == DialogResult.OK)
			{
				LoadFromFile (ofd.FileName);
			}
		}

		void LoadFromFile (string a_filename)
		{
			string s = "";

			ArrayList buff_array = new ArrayList (16);

			StreamReader sr = new StreamReader (a_filename);

			s = sr.ReadLine ();
			while (s != null)
			{	// very primitive name check
				if (!MainForm.ValidateName (s))
				{
					DialogResult dr = MessageBox.Show ("Invalid computer name \""+s.Substring (0,128)+"\" \r\nThe name will be ignored.\r\n"
						+"Do you want to continue to process the file?", "Adding Targets from File", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
					if (dr == DialogResult.No)
					{
						return;
					}
				}
				else 
				{
					buff_array.Add (s.ToUpper ());
				}

				s = sr.ReadLine ();
			}

			foreach (string t in buff_array)
			{
				if (MainForm.Cfg.AddTarget (t))
				{
					lbTargets.Items.Add (t);
				}
			}
		}

		private void tbNewTarget_TextChanged(object sender, System.EventArgs e)
		{
			btAddTarget.Enabled = (tbNewTarget.Text.Length > 0);
		}

		private void lbTargets_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			btRemoveTarget.Enabled = (lbTargets.SelectedIndex >= 0);
		}

		protected override void btCancel_Click(object sender, System.EventArgs e)
		{
			DialogResult dr = MessageBox.Show ("Quitting at this point will leave Repository Database installed, but SQLH2 Collector will not be cofigured properly. \r\n"
				+"Do you want to quit Wizard?", "Wizard has not completed", MessageBoxButtons.YesNoCancel);

			if (dr == DialogResult.OK)
			{
				base.btCancel_Click (sender, e);
			}
		}

		void SaveConfig ()
		{
				MainForm.Cfg.SetRepository (MainForm.stRep.Server, MainForm.stRep.Database);
				MainForm.Cfg.SetTargets ();
				MainForm.Cfg.Save ();
		}

		protected override void btNext_Click(object sender, System.EventArgs e)
		{
			try
			{
				SaveConfig ();
				base.btNext_Click (sender, e);
			}
			catch (Exception ex)
			{
				MessageBox.Show (ex.Message, "Saving XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

	}
}

